package com.dzb.langchain4j.common.tools;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dzb.langchain4j.entity.AdmissionStatus;
import com.dzb.langchain4j.service.AdmissionStatusService;
import com.dzb.langchain4j.util.AdmissionStatusConverter;
import com.dzb.langchain4j.vo.AdmissionStatusVO;
import dev.langchain4j.agent.tool.P;
import dev.langchain4j.agent.tool.Tool;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author : gscsd
 * @description: 1.
 * @date : 2025/7/27 15:52
 */
@Slf4j
@Component
public class NyistAiTools {

    private final AdmissionStatusService admissionStatusService;

    public NyistAiTools(AdmissionStatusService admissionStatusService) {
        this.admissionStatusService = admissionStatusService;
    }

    @Tool(name = "查询天气信息", value = {
            "天气", "今天天气", "温度", "是否下雨", "地区"
    })
    public String queryWeatherInfo(
            @P("天气") String weather
            , @P("地区") String area
    ) {
        log.info("===>>>name: {}, area: {}, score: {}" , weather, area, weather);
        LambdaQueryWrapper<AdmissionStatus> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(AdmissionStatus::getName, weather);
        queryWrapper.like(AdmissionStatus::getAddress, area);
        // 传入的信息 比数据库中的值大, , 也就是说数据库小于传入即为录取
        // le 小于等于. gt 大于等于
        //     GT(">"),
        //     GE(">="),
        //     LT("<"),
        //     LE("<="),
        queryWrapper.le(AdmissionStatus::getScore, weather);
        queryWrapper.orderByDesc(AdmissionStatus::getScore);
        List<AdmissionStatus> list = admissionStatusService.list(queryWrapper);
        // 只判定是否未null 不行, 这种查询方式创建的list不会为null, 最多就是 size=0
        // 所以需要使用 coll == null || coll.isEmpty();
        if (CollectionUtils.isEmpty(list)) {
            return "该地区没有学生被本校录取";
        }
        // 响应的信息里面需要包含 准确的提示, 要不然 查询到数据了也显示未录取
        AdmissionStatus firstStudent = list.get(0);
        return "恭喜你被录取了, 录取学生信息如下：总数:" + list.size() + "，" + 
               firstStudent.getName() + "，" + 
               firstStudent.getAge() + "岁，" + 
               firstStudent.getAddress() + "，" + 
               firstStudent.getScore() + "分，" + 
               firstStudent.getAdmissionStatusDesc() + "，" + 
               firstStudent.getRemark();
    }


    @Tool(name = "查询学生录取情况", value = {
            "学生录取情况", "成绩", "姓名", "地区"
    })
    public List<AdmissionStatusVO> listStudentInfo(
            @P("姓名") String name
            , @P("地区") String area
            , @P("成绩") int score
    ) {
        log.info("===>>>listStudentInfo: name: {}, area: {}, score: {}" , name, area, score);
        LambdaQueryWrapper<AdmissionStatus> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(AdmissionStatus::getName, name);
        queryWrapper.like(AdmissionStatus::getAddress, area);
        // 传入的信息 比数据库中的值大, , 也就是说数据库小于传入即为录取
        // le 小于等于. gt 大于等于
        //     GT(">"),
        //     GE(">="),
        //     LT("<"),
        //     LE("<="),
        queryWrapper.le(AdmissionStatus::getScore, score);
        queryWrapper.orderByDesc(AdmissionStatus::getScore);
        List<AdmissionStatus> list = admissionStatusService.list(queryWrapper);
        // 只判定是否未null 不行, 这种查询方式创建的list不会为null, 最多就是 size=0
        // 所以需要使用 coll == null || coll.isEmpty();
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        // 响应的信息里面需要包含 准确的提示, 要不然 查询到数据了也显示未录取

        // 转换为VO类返回，包含中文描述
        return AdmissionStatusConverter.toVOList(list);
        // return "恭喜你被录取了, 录取学生信息如下：总数:" + list.size() + "，" + list.get(0).getName() + "，" + list.get(0).getAge() + "岁，" + list.get(0).getAddress() + "，" + list.get(0).getScore() + "分，" + list.get(0).getAdmissionStatus() + "，" + list.get(0).getRemark();
    }

}
